package com.example.ccj.b.c.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.StringUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 自定义选择控件，包含单选和多选的样式
 * isSelected设置是否选中
 * showText 选中的文本
 * isRight 这个选项是否正确
 * isCheck 这个选项是否检查对错，这个要设置为true  isRight 才会发挥作用
 * tag 选项的编号  A\B\C\D
 */
public class MRadioButton <T> extends LinearLayout implements View.OnClickListener{

    public static final int SINGLE = 0;
    public static final int MULTIPLE = 1;


    long clickTime = 0;
    int checkInterval = 300;//两次点击间隔
    boolean onlyOne = false;

    OnClickListener onClickListenerN = new OnClickListener() {
        @Override
        public void onClick(View v) {
            //点击回调
            setOnClickSelected(!isSelected);
        }
    };

    @Override
    public void onClick(View v) {
        if (onlyOne) {
            if (onClickListenerN != null) onClickListenerN.onClick(v);
            setOnClickListener(null);
        } else if (!(System.currentTimeMillis() - clickTime < checkInterval)) {
            clickTime = System.currentTimeMillis();
            if (onClickListenerN != null) onClickListenerN.onClick(v);
        }
    }

    @IntDef({SINGLE,MULTIPLE,})
    @Retention(RetentionPolicy.SOURCE)
    public @interface OptionChoiceMode {}

    //当前选项模式，单选或者多选
    int mode = SINGLE;
    //选项显示的图标
    MImageView mImageView;
    //选项编号控件
    TextView optionTag;
    //选项文本控件
    TextView optionText;
    //显示的选项文本
    CharSequence showText;
    //是否正确
    boolean isRight = false;
    //是否选中
    boolean isSelected;
    //是否检查对错，决定是否显示错误以及正确图标
    boolean isCheck;
    //选项文字文本大小
    int textSize = 18;
    //选项编号
    CharSequence tag;

    IMRadioButtonListener<T> radioButtonListener;

    T value;

    MRadioButton<T> mRadioButton;

    public MRadioButton(Context context){
        super(context);
        init(context);
        mRadioButton = this;
    }

    public MRadioButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MRadioButton);
        //选项类型
        mode = typedArray.getInteger(R.styleable.MRadioButton_m_rb_mode,0);
        //文本大小
        textSize = typedArray.getInteger(R.styleable.MRadioButton_m_rb_text_size,20);
        typedArray.recycle();

        init(context);
    }

    private void init(Context context){
        LinearLayout.inflate(context, R.layout.custom_m_radio_button,this);

        mImageView = findViewById(R.id.custom_m_radio_button_view);
        optionTag = findViewById(R.id.custom_m_radio_button_option_text);
        optionText = findViewById(R.id.custom_m_radio_button_option_content_text);

        optionText.setTextSize(textSize);
        optionText.setTextColor(getResources().getColor(R.color.m_radio_button_def_text_color));
    }

    /**
     * 点击后的处理逻辑
     * @param selected 点击后应改变的状态
     */
    public void setOnClickSelected(boolean selected) {
        //如果是单选项并且当前已是选中状态不做更改
        if (isSelected && mode == SINGLE) return;
        isSelected = selected;
        if (radioButtonListener != null) radioButtonListener.onCheckedChanged(mRadioButton,isSelected);
        check();
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
        check();
    }

    /**
     * 点击后的按钮及文字显示状态逻辑控制
     */
    public void check(){
        if (isSelected) {
            setSelectState();
        } else {
            optionTag.setVisibility(VISIBLE);
            optionText.setTextColor(getResources().getColor(R.color.m_radio_button_def_text_color));
            if (mode == SINGLE)  mImageView.setImageResource(R.mipmap.single_option_choice_def);
            if (mode == MULTIPLE)  mImageView.setImageResource(R.mipmap.multiple_select_def);
        }
    }

    /**
     * 设置选择后的按钮及文字状态
     */
    public void setSelectState(){
        optionTag.setVisibility(GONE);
        if (isCheck) {
            //检查后不能再次点击
            if (isRight) {
                optionText.setTextColor(getResources().getColor(R.color.m_radio_button_right_text_color));
                if (mode == SINGLE) mImageView.setImageResource(R.mipmap.single_option_choice_right);
                if (mode == MULTIPLE) mImageView.setImageResource(R.mipmap.multiple_select_right);
            } else {
                optionText.setTextColor(getResources().getColor(R.color.m_radio_button_wrong_text_color));
                if (mode == SINGLE) mImageView.setImageResource(R.mipmap.single_option_choice_wrong);
                if (mode == MULTIPLE) mImageView.setImageResource(R.mipmap.multiple_select_wrong);
            }
        } else {
            optionText.setTextColor(getResources().getColor(R.color.m_radio_button_selected_text_color));
            if (mode == SINGLE)  mImageView.setImageResource(R.mipmap.single_option_choice_selected);
            if (mode == MULTIPLE)  mImageView.setImageResource(R.mipmap.multiple_selected);
        }
    }

    @SuppressLint("ResourceAsColor")
    public void build(){
        super.setOnClickListener(this);
        this.optionText.setText(showText);
        this.optionTag.setText(tag);
        if (isCheck) setReadOnly();
        setSelected(isSelected);
    }

    public MRadioButton<T> setRadioButtonListener(T t, IMRadioButtonListener<T> radioButtonListener) {
        this.value = t;
        this.radioButtonListener = radioButtonListener;
        if (radioButtonListener != null) {
            CharSequence optionText1 = radioButtonListener.getOptionText(value);
            if (!StringUtils.isEmpty(optionText1)) showText = optionText1;
            CharSequence optionTag = radioButtonListener.getTag(value);
            if (!StringUtils.isEmpty(optionTag)) this.tag = optionTag;
            isCheck = radioButtonListener.isCheck(value);

            isRight = radioButtonListener.checkIsCorrect(value);
//            setSelected(radioButtonListener.isSelected(value));
            isSelected = radioButtonListener.isSelected(value);
        }
        return this;
    }


    public T getValue() {
        return value;
    }

    public void setReadOnly(){
        super.setOnClickListener(null);
    }

    @Override
    public CharSequence getTag() {
        return tag;
    }

    public MRadioButton<T> setTag(CharSequence tag1) {
        this.tag = tag1;
        return this;
    }

    public int getMode() {
        return mode;
    }

    public MRadioButton<T> setMode(@OptionChoiceMode int mode) {
        this.mode = mode;
        return this;
    }

    public boolean isRight() {
        return isRight;
    }

    public MRadioButton<T> setRight(boolean right) {
        isRight = right;
        return this;
    }

    public CharSequence getShowText() {
        return showText;
    }

    public MRadioButton<T> setShowText(CharSequence showText) {
        this.showText = showText;
        return this;
    }

    public boolean isSelected() {
        return isSelected;
    }
}
